package com.jwthhealth_pub.common.base;

import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroupOverlay;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.jwthhealth_pub.R;

/**
 * base activity
 * Created by liuzhen on 2017/12/13.
 */

public abstract class BaseActivity extends AppCompatActivity {

    private ProgressBar v;
    public boolean isProgressBarShow = false;
    private boolean isToast = true;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        title();
    }

    public void showLoadProgresbar() {

        isProgressBarShow = true;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //api 19
            ViewGroup parent = (ViewGroup) getWindow().getDecorView().getRootView();
            ViewGroupOverlay mProgressbarOverlay = parent.getOverlay();

            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int screenWidth = displaymetrics.widthPixels;
            int screenHeight = displaymetrics.heightPixels;

            View mProegressbarView = new ProgressBar(this);
            mProegressbarView.layout(screenWidth / 2 - 50, screenHeight / 2 - 50, screenWidth / 2 + 50, screenHeight / 2 + 50);
            mProgressbarOverlay.add(mProegressbarView);
        } else {
            // < api 19
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int screenWidth = displaymetrics.widthPixels;
            int screenHeight = displaymetrics.heightPixels;

            WindowManager.LayoutParams mWindowParams = new WindowManager.LayoutParams();
            mWindowParams.gravity = Gravity.TOP;
            mWindowParams.x = screenWidth / 2 - 50;
            mWindowParams.y = screenHeight / 2 - 50;
            mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
            mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
            mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
//                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
//                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
//                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
            mWindowParams.format = PixelFormat.TRANSLUCENT;
            mWindowParams.windowAnimations = 0;
            mWindowParams.gravity = Gravity.TOP | Gravity.START;

            v = new ProgressBar(this);
//        int backGroundColor = getResources().getColor(R.color.primarycolor);
//        v.setBackgroundColor(backGroundColor);
//        v.setImageBitmap(bm);

//        mWindowManager = (WindowManager)getSystemService("window");
            WindowManager windowManager = getWindowManager();
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1)
                windowManager.addView(v, mWindowParams);
        }
    }

    public void missLoadProgressbar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().getDecorView().getRootView().getOverlay().clear();
        } else {
            WindowManager windowManager = getWindowManager();
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1)
                windowManager.removeView(v);
        }

        isProgressBarShow = false;
    }

    public void toas(final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isToast) {
                    Toast.makeText(BaseActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void errorToast() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BaseActivity.this, getString(R.string.common_net_error), Toast.LENGTH_SHORT).show();
            }
        });
    }

    protected abstract void title();
}
